package cn.lyvust.eais.manage.service.impl;

import cn.lyvust.eais.common.exception.RequestException;
import cn.lyvust.eais.manage.dto.param.rank.StudentAddParam;
import cn.lyvust.eais.manage.dto.param.rank.StudentParam;
import cn.lyvust.eais.manage.entity.Student;
import cn.lyvust.eais.manage.entity.Teacher;
import cn.lyvust.eais.manage.mapper.StudentMapper;
import cn.lyvust.eais.manage.service.StudentService;
import cn.lyvust.eais.manage.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ggzheng
 * @since 2021-04-04
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
    @Autowired
    public TeacherService teacherService;

    @Override
    @Transactional
    public boolean add(StudentAddParam dto) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cert_no", dto.getCertNo());
        Student old = this.getOne(wrapper);
        if (old != null) {
            throw RequestException.fail("添加失败,该生已被注册过,不能重复添加!");
        }
        String teacherNo = dto.getTeacherNo();
        if (StringUtils.isEmpty(teacherNo)) {
            throw RequestException.fail("添加失败,招生教师的教工编号不能为空!");
        }
        Teacher teacher = teacherService.getOne(new QueryWrapper<Teacher>().eq("no", teacherNo));
        if (teacher == null) {
            throw RequestException.fail("添加失败,招生教师的信息不存在,请联系管理员进行添加!");
        }
        //添加学生
        Student entity = new Student();
        BeanUtils.copyProperties(dto, entity);
        this.save(entity);
        //更新招生教师的招生总数
        return this.statTotal(teacherNo);
    }

    @Override
    @Transactional
    public boolean del(StudentParam dto) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("username", dto.getUsername());
        List resList = this.list(wrapper);
        if (resList == null || resList.size() == 0) {
            throw RequestException.fail("学生信息不存在!");
        }
        if (resList.size() >= 2 && StringUtils.isEmpty(dto.getCertNo())) {
            throw RequestException.fail("学生重名,请输入学生的身份证号码!");
        }
        if (StringUtils.isNotEmpty(dto.getCertNo())) {
            wrapper.eq("cert_no", dto.getCertNo());
        }
        resList = this.list(wrapper);
        if (resList.size() == 1) {
            Student student = (Student) resList.get(0);
            this.removeById(student);
            this.statTotal(dto.getTeacheNo());
        } else {
            throw RequestException.fail("删除失败,请联系管理员!");
        }
        return true;
    }

    @Override
    public boolean statTotal(String teacherNo) {
        Teacher teacher = teacherService.getOne(new QueryWrapper<Teacher>().eq("no", teacherNo));
        if (teacher == null) {
            throw RequestException.fail("更新失败,教师的信息不存在,请联系管理员进行核对!");
        }
        //更新招生教师的招生总数
        List studentList = this.list(new QueryWrapper<Student>().eq("teacher_no", teacherNo));
        teacher.setTotal(studentList.size());
        return teacherService.updateById(teacher);
    }
}
